package org.egrit.image;

import java.awt.Point;
import java.awt.image.WritableRenderedImage;

import org.egrit.IGrid;
import org.egrit.IGridBounds;
import org.egrit.IGridFactory;
import org.egrit.IGridIterator;

import org.jaitools.imageutils.ImageUtils;
import org.jaitools.imageutils.iterator.WritableSimpleIterator;

/**
 *
 * @author michael
 */
public class GridFactory implements IGridFactory {

    @Override
    public IGrid create(IGrid sourceGrid, IGridBounds bounds) {
        WritableRenderedImage image = ImageUtils.createConstantImage(
                bounds.getMinGridX(), bounds.getMinGridY(), 
                bounds.getWidth(), bounds.getHeight(), 
                sourceGrid.getOutsideValue());
        
        WritableSimpleIterator imageIter = new WritableSimpleIterator(image, null, null);
        
        IGridIterator gridIter = bounds.getIterator();
        do {
            Point pos = gridIter.getPos();
            imageIter.setSample(pos, sourceGrid.getGridValue(pos));
        } while (gridIter.next());
        
        return new ImageBackedGrid(image, sourceGrid.getOutsideValue());
    }

    
}
